
package com.mego.tzadd.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.mego.tzadd.R;
import com.mego.tzadd.database.dbmodel.Jtdetailinfo;
import com.mego.tzadd.database.manager.JtinfoCenter;
import com.mego.tzadd.database.manager.SystemCenter;
import com.mego.tzadd.utils.HttpRequest;
import com.mego.tzadd.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import zuo.biao.library.base.BaseFragment;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.EditTextInfoActivity;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.StringUtil;


public class JtInfoFragment0 extends BaseFragment implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "JtInfoFragment0";

    //与Activity通信<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 创建一个Fragment实例
     *
     * @return
     */
    public static JtInfoFragment0 createInstance() {
        return new JtInfoFragment0();
    }

    //与Activity通信>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.jtinfofragment0);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        return view;//返回值必须为view
    }

    private ImageView iv_jtlbzd;
    private TextView tv_jtname;
    private EditText edit_bjje;
    private TextView tv_deviceid;
    private TextView tv_expiration_date;

    @Override
    public void initView() {//必须在onCreateView方法内调用
        tv_jtname = (TextView) findViewById(R.id.tv_jtname);
        tv_deviceid = (TextView) findViewById(R.id.tv_deviceid);
        edit_bjje = (EditText) findViewById(R.id.edit_bjje);
        tv_expiration_date = (TextView) findViewById(R.id.tv_expiration_date);

        iv_jtlbzd = (ImageView) findViewById(R.id.iv_jtlbzd);

    }

    public Jtdetailinfo _jtinfo = null;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreateView方法内调用
        _jtinfo = JtinfoCenter.getInstance().jtdetailinfo;
        tv_jtname.setText(_jtinfo.getName());
        _jtname = _jtinfo.getName();
        _bjje = _jtinfo.getBjje();
        edit_bjje.setText(_bjje + "");

        tv_deviceid.setText("設備ID: " + _jtinfo.getJt_serial());

        String expdate = _jtinfo.getExpiration_date();
        if (_jtinfo.getStart_date().isEmpty()) {
            expdate = "未正式上線";
        } else if (expdate.compareTo("2099-12-31") == 0) {
            expdate = "未限制";
        }
        tv_expiration_date.setText("到期時間:" + expdate);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis() + 31 * 24 * 3600 * 1000L);
        String strDate = formatter.format(curDate);
        if (strDate.compareTo(_jtinfo.getExpiration_date()) > 0) {
            Toast.makeText(getContext(), "此機台 " + _jtinfo.getExpiration_date() + " 到期！", Toast.LENGTH_LONG).show();
        }

    }
    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreateView方法内调用
        findViewById(R.id.tv_czjl).setOnClickListener(this);
        findViewById(R.id.tv_deljt).setOnClickListener(this);
        findViewById(R.id.tv_shezhi).setOnClickListener(this);
        findViewById(R.id.tv_jtname).setOnClickListener(this);

        findViewById(R.id.iv_jtlbzd).setOnClickListener(this);

    }

    private final int REQUEST_TO_EDIT_NAME = 33;
    private final int REQUEST_TO_EDIT_BJJE = 34;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQUEST_TO_EDIT_NAME:
                if (data != null) {
                    String name = StringUtil.getTrimedString(
                            data.getStringExtra(EditTextInfoWindow.RESULT_VALUE));
                    _jtname = name;
                    tv_jtname.setText(name);
                    _jtinfo.setName(name);

//                    doXgjtname();
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_czjl:
                onCzjl();
                break;
            case R.id.tv_deljt:
                onDeljt();
                break;
            case R.id.tv_shezhi:
                onShezhi();
                break;
            case R.id.tv_jtname:
                editName(true);
                break;
            case R.id.iv_jtlbzd:
                onZd();
                break;
        }
    }

    private void onZd() {
        new AlertDialog(getContext(), "", "確認置頂?", "確認", "取消", 14, this).show();
    }

    String _jtname = "";

    private void editName(boolean toWindow) {
        if (toWindow) {
            intent = EditTextInfoWindow.createIntent(context, EditTextInfoWindow.TYPE_NAME
                    , "機台名稱", _jtname, context.getPackageName());
        } else {
            intent = EditTextInfoActivity.createIntent(context, EditTextInfoActivity.TYPE_NAME
                    , "機台名稱", _jtname);
        }

        toActivity(intent, REQUEST_TO_EDIT_NAME, !toWindow);
    }

    int _bjje = 0; //保夹金额

    private void onShezhi() {
        if (_issend) {
            showShortToast("設置进行中...");
            return;
        }

        if (edit_bjje.getText().toString().isEmpty()) {
            showShortToast("設置保夾金額錯誤!");
            return;
        }

        _bjje = Integer.parseInt(edit_bjje.getText().toString());

        if (_bjje < 0 || _bjje > 10000) {
            showShortToast("設置保夾金額錯誤!");
            return;
        }

        new AlertDialog(getContext(), "", "確認設置此機臺?", true, 11, this).show();

    }

    private void onDeljt() {
        new AlertDialog(getContext(), "", "確認刪除此機臺?", true, 13, this).show();
    }


    private void onCzjl() {
        startActivity(CzjlActivity.createIntent(getContext()));
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }
        if (requestCode == 11) {
            //上传信息
            doSend();
        } else if (requestCode == 13) {
            //上传信息
            deleteJt();
        } else if (requestCode == 14) {
            //機台列表置頂
            doZd();
        }
    }

    private void doZd() {
        showProgressDialog(R.string.loading);
        long adminid = SystemCenter.getInstance().adminid;
        try {
            HttpRequest.jtorderzd(_jtinfo.getJtid(), adminid,0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("機台置頂成功！");
                        iv_jtlbzd.setImageResource(R.mipmap.btn_turn_on);
                    } else {
                        showShortToast("機台無回應訊息！");
                    }
                    dismissProgressDialog();
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }
    }

    boolean _issend = false;

    private void doSend() {

        int max = 4;
        if (Utils.isHasChinese(_jtname)) {
            max = 2;
        }

        if (_jtname.length() < max) {
            showShortToast("請輸入2個中文或4個英數字以上機台名稱！");
            return;
        }
        _jtinfo.setBjje(_bjje);
        _issend = true;
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.setbjje(_jtinfo.getJtid(), _jtinfo.getBjje(), _jtname, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("機台設置成功訊息！");
                    } else {
                        showShortToast("機台無回應訊息！");
                    }
                    dismissProgressDialog();
                    _issend = false;
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
            _issend = false;
        }
    }

    private void doXgjtname() {
//        _jtinfo.setBjje(_bjje);
//        _jtinfo.setName(_jtname);


//        showProgressDialog(R.string.loading);
        try {
            HttpRequest.xgjtname(_jtinfo.getJtid(), _jtname, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
//                    if (code == 1) {
//                        showShortToast("設置成功！");
//                    } else {
//                        showShortToast("設置失敗！");
//                    }
//                    dismissProgressDialog();
                }
            });
        } catch (Exception e1) {
//            showShortToast("網絡無連接！");
//            dismissProgressDialog();
        }
    }

    private void deleteJt() {

        showProgressDialog(R.string.loading);
        try {
            HttpRequest.deljt(_jtinfo.getJtid(), 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("刪除機臺成功！");
                    } else {
                        showShortToast("此機臺已刪除！");
                    }
                    dismissProgressDialog();
                    getActivity().finish();
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }
    }

}